package com.zhz.project.main.socolar.wenzhang.util;

import java.util.ArrayList;
import java.util.List;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.nodes.TextNode;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.Span;
import org.htmlparser.util.NodeList;

import com.zhz.project.common.util.alibaba.StringUtil;
import com.zhz.project.main.socolar.wenzhang.WenzhangRecord;

public class SocolarWenzhangParser {

    String content = null;

    public List<WenzhangRecord> parse() {

        List<WenzhangRecord> recordList = new ArrayList<WenzhangRecord>();
        try {
            Parser parser = new Parser(content);

            NodeFilter[] nodeFilter = new NodeFilter[2];

            nodeFilter[0] = new TagNameFilter("li");

            nodeFilter[1] = new HasAttributeFilter("class", "pr");

            AndFilter andFilter = new AndFilter(nodeFilter);

            NodeList list = parser.extractAllNodesThatMatch(andFilter);

            for (int i = 0; i < list.size(); i++) {

                WenzhangRecord record = new WenzhangRecord();
                Node node = list.elementAt(i);

                NodeList children = node.getChildren();

                for (int k = 0; k < children.size(); k++) {

                    Node child = children.elementAt(k);

                    if (child instanceof LinkTag) {

                        LinkTag linkTag = (LinkTag) child;
                        String title = linkTag.getAttribute("class");
                        String onclick = linkTag.getAttribute("onclick");
                        if (StringUtil.isNotBlank(title)) {
                            record.setTitle(StringUtil.trim(linkTag.getLinkText()));
                            record.setTitleurl(StringUtil.substring(linkTag.getLink(), 0,
                                StringUtil.indexOf(linkTag.getLink(), "&key")));
                        }
                        if (StringUtil.isNotBlank(onclick)) {

                            int begin = StringUtil.indexOf(onclick, "('");
                            int end = StringUtil.indexOf(onclick, "&key");
                            record.setFulltexturl(StringUtil.substring(onclick, begin + 2, end));
                        }
                        if (StringUtil.isBlank(title) && StringUtil.isBlank(onclick)) {
                            record.setAbstracturl(StringUtil.substring(linkTag.getLink(), 0,
                                StringUtil.indexOf(linkTag.getLink(), "&key")));
                        }

                    }
                    if (child instanceof TextNode) {
                    }
                    if (child instanceof Span) {
                        Span span = (Span) child;
                        String author = span.getAttribute("class");
                        if (StringUtil.equals(author, "Au")) {
                            record.setAuthor(span.toPlainTextString());
                        }
                    }
                }
                recordList.add(record);
            }
            return recordList;
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return null;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

}
